package com.nintendo.frame;

import com.nintendo.entity.pojo.Order;
import com.nintendo.mapper.OrderDao;
import com.nintendo.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.List;

public class CheckOutFrame extends JFrame {

    private JTable orderTable;
    private DefaultTableModel tableModel;

    public CheckOutFrame() {
        setTitle("结账管理");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建表格模型
        tableModel = new DefaultTableModel(
                new Object[]{"ID", "订单号", "桌号", "创建时间", "金额", "客户ID", "状态", "人数"},
                0
        );

        // 创建表格并设置模型
        orderTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(orderTable);
        add(scrollPane, BorderLayout.CENTER);

        // 添加鼠标点击事件监听器
        orderTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
//                System.out.println(e);
                int selectedRow = orderTable.getSelectedRow();
//                System.out.println("selectedRow===>" + selectedRow);
                if (selectedRow >= 0) {
                    Long orderId = (Long) tableModel.getValueAt(selectedRow, 0);
                    openOrderDetail(orderId);
                }
            }
        });

        // 加载数据
        loadOrders();

        setVisible(true);
    }

    private void loadOrders() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        try {
            OrderDao orderDao = sqlSession.getMapper(OrderDao.class);
            List<Order> orders = orderDao.selectAll();
            System.out.println(orders);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            for (Order order : orders) {
                // 添加调试信息
                System.out.println("Order ID: " + order.getId());
                System.out.println("Order No: " + order.getOrderNo());
                System.out.println("Desk ID: " + order.getDeskId());
                System.out.println("Create Time: " + order.getCreateTime());
                System.out.println("Money: " + order.getMoney());
                System.out.println("Customer ID: " + order.getCustomerid());
                System.out.println("Status: " + order.getStatus());
                System.out.println("Number: " + order.getNumber());

                String createTimeStr = order.getCreateTime() != null ? dateFormat.format(order.getCreateTime()) : "N/A";
                tableModel.addRow(new Object[]{
                        Long.valueOf(order.getId()),  // 将 Integer 转换为 Long
                        order.getOrderNo(),
                        order.getDeskId(),
                        createTimeStr,
                        order.getMoney(),
                        order.getCustomerid(),
                        order.getStatus(),
                        order.getNumber()
                });
            }
        } finally {
            sqlSession.close();
        }
    }

    private void openOrderDetail(Long orderId) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        try {
            OrderDao orderDao = sqlSession.getMapper(OrderDao.class);
            Order order = orderDao.selectById(orderId);
            if (order != null) {
                new OrderDetailFrame(order);
            } else {
                JOptionPane.showMessageDialog(this, "订单未找到", "错误", JOptionPane.ERROR_MESSAGE);
            }
        } finally {
            sqlSession.close();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new CheckOutFrame());
    }


    public void refreshTable() {
        tableModel.setRowCount(0);
        loadOrders();
    }



}
